package org.dllwh.mongodb;

import jakarta.annotation.Resource;
import org.dllwh.mongodb.model.SysUser;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 把今天最好的表现当作明天最新的起点．．～
 * <p>
 * Today the best performance as tomorrow the newest starter!
 *
 * @ClassName RemoveTest
 * @Description MongoTemplate 删除操作测试类
 * @Author: <a href="mailto:duleilewuhen@sina.com">独泪了无痕</a>
 * @Date 2025-05-10 00:45
 * @版本 V 1.0.1
 * @since JDK 17
 */
@SpringBootTest
public class RemoveTest {
    @Resource
    private MongoTemplate mongoTemplate;
    private final String collectionName = "RemoveTest";

    @BeforeEach
    public void init() {
        // 删除集合
        mongoTemplate.dropCollection(collectionName);
        List<SysUser> userList = new ArrayList<>();
        userList.add(new SysUser("1", "白鹿", "女", 31));
        userList.add(new SysUser("2", "陈都灵", "女", 32));
        userList.add(new SysUser("3", "高圆圆", "女", 46));
        userList.add(new SysUser("4", "张含韵", "女", 36));
        userList.add(new SysUser("5", "李沁", "女", 35));
        userList.add(new SysUser("6", "胡歌", "男", 43));
        userList.add(new SysUser("7", "刘亦菲", "女", 38));
        userList.add(new SysUser("8", "成龙", "男", 71));
        userList.add(new SysUser("9", "杨幂", "男", 39));
        userList.add(new SysUser("10", "陈钰琪", "女", 33));
        mongoTemplate.insert(userList, collectionName);
    }

    @Test
    public void removeOne() {
        Query query = new Query(Criteria.where("id").is("1"));
        SysUser sysUser = mongoTemplate.findOne(query, SysUser.class, collectionName);
        if (null != sysUser) {
            mongoTemplate.remove(sysUser, collectionName);
        }
        long count = mongoTemplate.count(query, collectionName);
        System.out.println("count = " + count);
    }

    @Test
    public void removeAll() {
        Query query = new Query(Criteria.where("age").gte(30));
        long count = mongoTemplate.count(query, collectionName);
        System.out.println("删除前，count=" + count);
        // 删除所有匹配的文档
        mongoTemplate.remove(query, collectionName);
        count = mongoTemplate.count(query, collectionName);
        System.out.println("删除后，count=" + count);
    }


    @Test
    public void removeLimit() {
        Query query = new Query(Criteria.where("age").gte(30));
        long count = mongoTemplate.count(query, collectionName);
        System.out.println("删除前，count=" + count);
        // 删除前两个文档
        mongoTemplate.remove(query.limit(2), collectionName);
        count = mongoTemplate.count(query, collectionName);
        System.out.println("删除后，count=" + count);
    }

    /**
     * 删除符合条件的单个文档，并返回删除的文档
     */
    @Test
    public void findAndRemoveTest() {
        Query query = new Query(Criteria.where("id").is(2));
        SysUser sysUser = mongoTemplate.findAndRemove(query, SysUser.class);
        System.out.println("删除的文档数据：" + sysUser.toString());
    }

    /**
     * 删除符合条件的所有文档，并返回删除的文档
     */
    @Test
    public void findAllAndRemoveTest() {
        Query query = new Query(Criteria.where("age").gte(30));
        long count = mongoTemplate.count(query, collectionName);
        System.out.println("删除前，count=" + count);
        // 删除所有匹配的文档
        List<SysUser> sysUserList = mongoTemplate.findAllAndRemove(query, collectionName);
        System.out.println("删除的数据：" + Arrays.toString(sysUserList.toArray()));
        count = mongoTemplate.count(query, collectionName);
        System.out.println("删除后，count=" + count);
    }

    @Test
    public void findAllAndRemoveLimit() {
        Query query = new Query(Criteria.where("age").gte(30));
        long count = mongoTemplate.count(query, collectionName);
        System.out.println("删除前，count=" + count);
        // 删除前两个文档
        List<SysUser> sysUserList = mongoTemplate.findAllAndRemove(query.limit(2), collectionName);
        System.out.println("删除的数据：" + Arrays.toString(sysUserList.toArray()));
        count = mongoTemplate.count(query, collectionName);
        System.out.println("删除后，count=" + count);
    }
}
